package com.century.sjt.window;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.activity.Forget_pwdPayActivity;
import com.century.sjt.util.Base64Utils;
import com.century.sjt.widget.gridpasswordview.GridPasswordView;


/**
 * Created by 炳基 on 2016/7/26.
 */
public class PayPwdWindow extends PopupWindow{
    private Context mContext;

    private GridPasswordView gpvCashPwd;
    private Button btn_ok;
    private View mMenuView;
    private TextView forger_pwd;

    private RequestQueue mQueue;
    private PayPwdWindowListItemClickListener listener;

    public PayPwdWindow(Activity context) {
        super(context);
        this.mContext= context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.sjt_paypwd_window, null);
        mQueue = Volley.newRequestQueue(mMenuView.getContext());
        gpvCashPwd=(GridPasswordView) mMenuView.findViewById(R.id.pay_cashpwd);
        forger_pwd=(TextView) mMenuView.findViewById(R.id.sjt_forget_pwd);
        forger_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("setpwd","3");
                intent.setClass(mContext, Forget_pwdPayActivity.class);
                mContext.startActivity(intent);
            }
        });

        btn_ok = (Button) mMenuView.findViewById(R.id.btn_payok);
        //确认按钮
        btn_ok.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                byte[] encodePwd = gpvCashPwd.getPassWord().getBytes();
                final String pwd = Base64Utils.encode(encodePwd);
                listener.onPassWord(pwd);
                listener.onCloseWindow(true);
                //销毁弹出框
                gpvCashPwd.clearPassword();
                dismiss();
            }
        });
        //设置按钮监听

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });
        gpvCashPwd.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onChanged(String psw) {

            }

            @Override
            public void onMaxLength(String psw) {
                byte[] encodePwd = psw.getBytes();
                final String pwd = Base64Utils.encode(encodePwd);
                listener.onPassWord(pwd);
                listener.onCloseWindow(true);
                gpvCashPwd.clearPassword();
                dismiss();
            }
        });
    }

    public void setPayPwdWindowListItemClickListener(PayPwdWindowListItemClickListener listener) {
        this.listener = listener;
    }
    public interface PayPwdWindowListItemClickListener{
        /**
         * 支付方式事件监听
         * @param
         * @return
         */
        void onPassWord(String pwd);
        boolean onOpenWindow(boolean isdo);
        boolean onCloseWindow(boolean isdo);

    }

}
